package socket.myRPC.proxy.impl;

import socket.myRPC.proxy.ClassInfoInjectionAware;
import socket.myRPC.rpc.Dispatcher;

import java.lang.reflect.Method;

public class LocalInvoke implements ClassInfoInjectionAware {
  
  private Class<?> proxyInterfaceInfo;
  
  @Override
  public void injectionClassInfo(Class<?> classInfo) {
    this.proxyInterfaceInfo = classInfo;
  }
  
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // 这里执行本地任务
    System.out.println("exec local task ......");
    
    // 拿到执行的 Class
    Object o = Dispatcher.get(proxyInterfaceInfo.getName());
    Class<?> clazz = o.getClass();
    // 拿到要执行的方法
    Method m = clazz.getMethod(method.getName(), method.getParameterTypes());
    // 通过 反射来执行方法
    return m.invoke(o, args);
  }
}
